package com.net.xpay.operator.controller.operator;

import com.google.common.base.Preconditions;
import com.net.xpay.operator.secutiry.RequestUser;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultList;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.operator.OperatorUser;
import com.net.xpay.common.domain.operator.OperatorUserBindLog;
import com.net.xpay.common.domain.operator.OperatorUserBinding;
import com.net.xpay.common.enums.operator.OperatorUserBindLogType;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.operator.OperatorUserBindLogManager;
import com.net.xpay.common.manager.operator.OperatorUserBindingManager;
import com.net.xpay.common.model.AddOperatorUserModel;
import com.net.xpay.common.service.OperatorUserBindingService;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.model.IdModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import com.net.xpay.operator.vo.OperatorUserListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/operator/operator-user-binding")
@Api(tags = "操盘手/用户关联表", description = "操盘手 基本信息 net")
public class OperatorUserBindingController {

    @Autowired
    private OperatorUserBindingManager operatorUserBindingManager;

    @Autowired
    private OperatorUserBindingService operatorUserBindingService;

    @Autowired
    private UserManager userManager;

    @Autowired
    private OperatorUserBindLogManager operatorUserBindLogManager;


    @ApiOperation("搜索关联公司列表")
    @PostMapping("/binding-company-list")
    @ResponseBody
    public ResultList<OperatorUserListVo> bindingCompanyList(@ApiIgnore @RequestUser OperatorUser operatorUser) {
        List<OperatorUserBinding> list = operatorUserBindingManager.getByOperatorIdList(operatorUser.getId());
        List<OperatorUserListVo> vos = BeanCopierUtil.batchCopy(OperatorUserBinding.class, OperatorUserListVo.class, list);
        for (OperatorUserListVo vo : vos) {
            User user = userManager.getById(vo.getUserId());
            vo.setName(user.getName());
        }
        ResultList<OperatorUserListVo> resultList = new ResultList<>(vos);
        resultList.setExtra(vos.size());
        return resultList;
    }


    @ApiOperation("添加关联公司")
    @PostMapping("/add-binding-company")
    @ResponseBody
    public ResultData<Boolean> addBindingCompany(@ApiIgnore @RequestUser OperatorUser operator, @RequestBody AddOperatorUserModel model) {
        operatorUserBindingService.addBindingCompany(operator.getId(),model);
        return new ResultData<>(true);
    }

    @ApiOperation("删除关联公司")
    @PostMapping("/delete-binding-user")
    @ResponseBody
    public ResultData<Boolean> deleteBindingUser(@ApiIgnore @RequestUser OperatorUser operatorUser, @RequestBody IdModel model) {
        Preconditions.checkArgument(model.getId() != null);
        if (operatorUser == null){
            throw new BusinessException(PoseidonErrorCode.UNKNOWN_ERROR, "用户不存在");
        }
        OperatorUserBinding operatorUserBinding = operatorUserBindingManager.findByOperatorUserIdAndUserId(operatorUser.getId(), model.getId());
        if (operatorUserBinding == null) {
            return new ResultData<>(true);
        }
        Preconditions.checkArgument(operatorUserBinding.getOperatorUserId().equals(operatorUser.getId())
         && operatorUserBinding.getUserId().equals(model.getId()));

        operatorUserBindingManager.deleteById(operatorUserBinding.getId());

        OperatorUserBindLog bindLog = new OperatorUserBindLog();
        bindLog.setType(OperatorUserBindLogType.DELETE);
        bindLog.setUserId(model.getId());
        bindLog.setOperatorUserId(operatorUser.getId());
        User user = userManager.getById(model.getId());
        bindLog.setUserName(user.getName());
        operatorUserBindLogManager.save(bindLog);
        return new ResultData<>(true);
    }
}
